<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>我要卖</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/statics/layui/css/layui.css" media="all">
</head>

<body>
  <blockquote class="layui-elem-quote">注意：您当前卖出币种 <span id="tipTypeId" style="color:green">A</span> 不能超过 <span id="tipMoney"
      style="color:red">1000</span> ！</blockquote>
  <br />
  <form class="layui-form" action="">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">卖出数量</label>
        <div class="layui-input-inline" style="width: 100px;">
          <input type="text" name="money" value="100" lay-verify="required|number" placeholder="￥" autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">接受币种</label>
      <div class="layui-input-inline" style="width: 150px;">
        <select id="buyTypeId" name="buyTypeId">
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <br />
        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      </div>
    </div>
  </form>
  <script src="/statics/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="../js/config.js"></script>
  <script type="text/javascript" src="../js/utils.js"></script>

  <script>
    var id = "", model = null;
    $(function () {
      id = Utils.getUrlParam("id");
      if (!id) {
        window.location.href = "../usercenter.html";
        return;
      }
      getMoneyInfo(id, function () {
        DataUtils.getMoneyTypeList(function (data) {
          if (data && data.length > 0) {
            $.each(data, function (i, item) {
              if (model.typeId != item.typeId) {
                $("#buyTypeId").append("<option value='" + item.typeId + "'>" + item.typeName + "</option>");
              }
            });
          }

          layui.use(['form'], function () {
            var form = layui.form, layer = layui.layer;

            //监听提交
            form.on('submit(demo1)', function (data) {
              var json = data.field;
              json.money = parseInt(json.money);
              json.typeId = model.typeId;
              var max = parseInt($("#tipMoney").text());
              if(json.money > max){
                layer.alert("卖出数量不能超过" + max, { title: '出错了', icon: 5 });
              }
              else{
                var typeName = $("#tipTypeId").text();
                layer.confirm('确定要卖出 '+json.money+' ('+typeName+') 吗?', {icon: 3, title:'提示'}, function(index){
                  var url = Config.WEB_SERVER_API + Config.URI.USER_MONEY_SELL_SAVE;
                  HttpUtils.httpPost(url, json, function (result, success) {
                    if (success) {
                      layer.alert('恭喜您，提交成功！', { title: '提示' }, function(index){
                        window.parent.location.reload();
                      });
                    } else {
                      layer.alert(result.resultMsg, { title: '出错了', icon: 5 });
                    }
                  });
                  layer.close(index);
                });                
              }
              return false;
            });

            //表单初始赋值
            // form.val('example', {"money":100});
          });
        });
      });
    });

    var getMoneyInfo = function (id, callback) {
      DataUtils.getUserMoneyInfo(id, function (data) {
        if (!data) {
          window.location.href = "../usercenter.html";
          return;
        }
        model = data;
        $("#tipTypeId").text(data.typeName);
        $("#tipMoney").text(data.money);
        if (callback) {
          callback();
        }
      });
    };


  </script>

</body>

</html>